package com.xiaocg.sparse.array;

/**
 * @author xiaocg
 * 基于数组实现queue
 */
public class ArrayQueue {
    private int maxSize;
    private int front=-1;
    private int rear=-1;
    private int[] arrays;

    public ArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        arrays = new int[maxSize];
    }

    /**
     * 判断队列是否已满
     */
    public boolean isFull(){
        return  rear == this.maxSize-1;
    }

    /**
     * 判断是否为空
     * @return
     */
    public boolean isEmpty(){
        return rear == front;
    }

    /**
     * 添加数据
     * @param n
     */
    public void add(int n){
        if (isFull()){
            System.out.println("队列已满,不能加入数据");
            return;
        }
        rear++;
        arrays[rear] = n;
    }

    /**
     * 取数据
     * @return
     */
    public int get(){
        if (isEmpty()){
            throw new RuntimeException("队列为空,不能取");
        }
        front++;
        return arrays[front];
    }

    /**
     * 打印队列数据
     */
    public void print(){
        if (isEmpty()){
            System.out.println("队列为空");
            return;
        }
        for (int i=0;i<arrays.length;i++){
            System.out.printf("%d\t",arrays[i]);
        }
    }

    /**
     * 获取头数据
     * @return
     */
    public int getHead(){
        if (isEmpty()){
            throw new RuntimeException("队列为空");
        }
        return arrays[front+1];
    }




}
